using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using RefreshModule;

namespace OnlineAnadolu
{ [Refresh()]
    public partial class UrunGoster : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (GridView1.Rows.Count == 0)
                {
                    Database db = new Database();
                    string query = "SELECT  products.productid, products.name, products.price, productimage.productimage FROM products INNER JOIN productimage ON products.imageid = productimage.productimageid where categoryid='"+Request.QueryString["CatId"].ToString()+"' ORDER BY products.productid DESC";
                    DataTable dt = db.select(query);
                    DataList1.DataSource = dt;
                    DataList1.DataBind();
                    
                    

                }
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    TreeView1.Nodes[0].Value = GridView1.Rows[i].Cells[0].Text;
                    FillAuthors(TreeView1.Nodes[0]);
                }
            }
        }
        void FillAuthors(TreeNode node)
        {
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select * From categories where categoryid='"+node.Value.ToString()+"'", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet authors = new DataSet();
            adapter.Fill(authors);
            if (authors.Tables.Count > 0)
            {
                foreach (DataRow row in authors.Tables[0].Rows)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();

                     newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();
                    newNode.CollapseAll();
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    node.ChildNodes.Add(newNode);
                    
                    FillTitlesForAuthors(newNode);
                }
            }
        }

        void FillTitlesForAuthors(TreeNode node)
        {
            string authorID = node.Value.ToString();
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select categoryname, categoryid From categories Where parentid = '" + authorID + "'", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet titlesForAuthors = new DataSet();
            adapter.Fill(titlesForAuthors);
            if (titlesForAuthors.Tables.Count > 0)
            {
                foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();

                    newNode.PopulateOnDemand = false;
                    newNode.CollapseAll();
                    SqlConnection connection1 = new SqlConnection(connString);
                    SqlCommand command1 = new SqlCommand("Select categoryname, categoryid From categories Where parentid = '" + (row["categoryid"]).ToString() + "'", connection1);
                    SqlDataAdapter adapter1 = new SqlDataAdapter(command1);
                    DataSet titlesForAuthors1 = new DataSet();
                    adapter1.Fill(titlesForAuthors1);
                    if (titlesForAuthors1.Tables.Count > 0)
                    {
                        newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();
                    }



                    // newNode.SelectAction = TreeNodeSelectAction.None;
                    node.ChildNodes.Add(newNode);
                    //node.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    FillTitlesForAuthors(newNode);
                }
            }
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "detay")
            {
                Session["idproduct"] = (string)e.CommandArgument;
                if(Session["username"].ToString()=="")
                Response.Redirect("UrunDetay.aspx");
                else
                Response.Redirect("UrunDetay2.aspx");

            }
        }
    }
}
